package cn.myjerry.others.commons;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.functors.SwitchTransformer;

/**
 * 函数式编程 Transformer 类型转换
 * 1、new Transformer() +transform
 * @author Jerry
 *
 */
public class Demo02 {
	public static void main(String[] args) {
		System.out.println("===自定义类型转换==");
		Predicate<Employee> isLow = new Predicate<Employee>() {
			@Override
			public boolean evaluate(Employee object) {
				return object.getSalary()<10000;
			}
		};
		Predicate<Employee> isHigh = new Predicate<Employee>() {
			@Override
			public boolean evaluate(Employee object) {
				return object.getSalary()>=10000;
			}
		};
		Predicate[] pres = {isLow, isHigh};
		Transformer<Employee, Level> lowTrans = new Transformer<Employee, Level>() {
			@Override
			public Level transform(Employee input) {
				return new Level(input.getName(), "卖身中...");
			}
		};
		Transformer<Employee, Level> highTrans = new Transformer<Employee, Level>() {
			@Override
			public Level transform(Employee input) {
				return new Level(input.getName(), "养身中...");
			}
		};
		Transformer[] trans = {lowTrans, highTrans};
		
		Transformer switchTrans = new SwitchTransformer(pres, trans, null);
		
		List<Employee> list = new ArrayList<>();
		list.add(new Employee("A", 9000));
		list.add(new Employee("B", 15000));
		
		Collection<Level> levelList = CollectionUtils.collect(list, switchTrans);
		
		Iterator<Level> leveIt = levelList.iterator();
		
		while(leveIt.hasNext()) {
			System.out.println(leveIt.next());
		}
		
	}
	/**
	 * 内置类型转换
	 */
	public static void inner() {
		System.out.println("===内置类型转换  长整形时间日期，转成指定格式的字符串==");
		Transformer<Long, String> tran = new Transformer<Long, String>() {
			@Override
			public String transform(Long input) {
				return new SimpleDateFormat("yyyy年MM月dd").format(input);
			}
		};
		List<Long> list = new ArrayList<>();
		list.add(100000000L);
		list.add(999999999999L);
		Collection<String> result = CollectionUtils.collect(list, tran);
		for(String temp:result) {
			System.out.println(temp);
		}
	}
}
